// 页面路由
const fs = require('fs');
const sql = require('../mysql/config')
//const path = require('path')
module.exports.publish = async function(req,res,next){
    console.log(req.file);
    console.log("--------");
    console.log(req.body);

    // res.send('ok');
    const {title,author,subhead,intro,content} = req.body;
    const sort_id = Number(req.body.sort_id);   
    let thumb;
    let isFile = false;
    if(req.file){
        const fileName = req.file.originalname.toLowerCase();
        const fileSize = req.file.size;
        const name = fileName.substr(fileName.lastIndexOf('.')+1,fileName.length);
        if(!/(gif|jpg|jpeg|png|bmp|webp)$/.test(name) || fileSize >= 1048576){
            //res.send("<script>alert('图片类型必须是.gif,jpeg,jpg,png,bmp,webp中的一种，且图片大小不能大于1M!')</script>");
            isFile = true;
        }else {
            const oldPath = req.file.path;
            const newPath = req.file.destination + req.file.originalname;
            fs.renameSync(oldPath,newPath);
            thumb = 'img/' + req.file.originalname;            
            isFile = false;
        }
    }else{
        thumb = 'img/wing.jpg'
    }

    if(isFile){
        res.send("<script>alert('图片类型必须是.gif,jpeg,jpg,png,bmp,webp中的一种，且图片大小不能大于1M!');location.href='/article';</script>");
       
    }else{
        try{
        await sql(`insert into ?? values (NULL,?,?,?,?,?,?,?,NULL,NULL,now(),now(),now())`,['article',sort_id,thumb,title,author,subhead,intro,content]);
        res.send("<script>alert('文章发布成功');location.href='/'</script>");                
    } catch (error){
        next(error);
    }
    }
    
}
let isDowns = true;
// 点赞
module.exports.downs = async function(req,res){
    console.log(req.body);  
    const id =  Number(req.body.downs);
    const data = await sql('select downs from article where article_id = ?',[id]);
    console.log(data[0].downs);
    let downs = data[0].downs === 'null'?0:data[0].downs;

    if (isDowns) {
        downs++;
        await sql('update article set downs = ? where article_id = ?',[downs,id]);
        res.send({
            code:200,
            msg:"点赞成功",
            downs,
            id
        });
        isDowns = false;
    }else{
        res.send({
            code:200,
            msg:"您已点赞，不要重复点赞!",
            downs
        });
    }
}
module.exports.downs = async function(req,res){
    console.log(req.body);  
    const id =  Number(req.body.id);
    const downs = req.body.downs;
    const isDowns = req.body.isDowns;
    const nickname = req.body.nickname;
    // 给点赞表管理点赞功能
    let thumbs = await sql("select * from thumbs_up where nickname = ? and article_id = ?",[nickname,id]);    
    if ( isDowns === 'false' ) {
        try {
            await sql('update article set downs = ? where article_id = ?',[downs,id]);
            
            if (thumbs.length === 0) {
                await sql("insert into thumbs_up values (NULL,?,?,?)",[nickname,id,1]);
            }else{
                await sql("update thumbs_up set on_off = ? where nickname = ?",[1,nickname]);
            }

            res.send({
                code:200,
                msg:"点赞成功"
            });
        } catch (error) {
            res.send({
                code:500,
                msg:"点赞失败"
            });
            next(error);
        }
    }else{
        try {
            await sql('update article set downs = ? where article_id = ?',[downs,id]);
            if (thumbs.length === 0) {
                await sql("insert into thumbs_up values (NULL,?,?,?)",[nickname,id,1]);
            }else{
                await sql("update thumbs_up set on_off = ? where nickname = ?",[0,nickname]);
            }
            res.send({
                code:200,
                msg:"取消点赞成功"
            });
        } catch (error) {
            res.send({
                code:500,
                msg:"点赞失败"
            });
            next(error);
        }
    }
    // res.send("ok");      
}